/**
 * 
 */
package de.fsujena.chat_pi.server;

import java.io.IOException;

import de.fsujena.chat_pi.message.SysMessage;
import de.fsujena.chat_pi.message.SysMessage.MESSAGE;
import de.fsujena.chat_pi.message.SysMessage.CATEGORY;

/**
 * @author Erik Hebestreit
 *
 */
public class SysMessageHandler {
	
	SysMessage message = null;
	
	MessageListener msgListener;

	/**
	 * 
	 */
	public SysMessageHandler(MessageListener msgListener) {
		this.msgListener = msgListener;
	}
	
	public boolean handleSysMessage(SysMessage message) {
		this.message = message;
		return filterCategory();		
	}

	private boolean filterCategory() {
		switch (message.getCategory()) {
		case CONNECTION:
			switch (message.getMessage()){
			case DISCONNECTED:
				msgListener.distributeMsg(message);
				msgListener.close();
				break;
			default:
				break;
			}
			break;
		case LOGIN:
			if (msgListener.loggedIn) {
				msgListener.sendSysMessage(CATEGORY.LOGIN, MESSAGE.ALREADY_LOGGED_IN);
				break;
			}
			
			if (message.getMessage() == MESSAGE.WANT_TO_LOGIN)
				return userLogin();
			if (message.getMessage() == MESSAGE.LOGGED_OUT){
				//msgListener.distributeMsg(message);
				msgListener.logout();
			}
			break;
			
		case REGISTRATION:
			if (message.getMessage() == MESSAGE.WANT_TO_REGISTER) {
				return register();
			}
			
		case STATUS:
			switch (message.getMessage()) {
			case REQUEST_USER_LIST:
				msgListener.sendUserList();
				break;

			default:
				break;
			}

		default:
			break;
		}
		
		return false;
		
	}

	private boolean register() {
		Object inputObject;
		String user = null, password = null;
		
		msgListener.sendSysMessage(CATEGORY.REGISTRATION, MESSAGE.REQUEST_NEW_USERNAME);
		
		try {
			inputObject = msgListener.connector.readGeneralObject();
		
		
			SysMessage sysMessage;
			if (inputObject instanceof SysMessage) {
				sysMessage = (SysMessage) inputObject;
				if (sysMessage.getMessage() == MESSAGE.SEND_NEW_USERNAME){
					user = sysMessage.getMessageString();
				} else {
					msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.MSG_NOT_EXPECTED);
					return false;
				}
			} else {
				msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.WRONG_MSG_TYPE);
				return false;
			}
			
			if (msgListener.serverObject.userManager.checkUserExists(user)) {
				msgListener.sendSysMessage(CATEGORY.REGISTRATION, MESSAGE.USERNAME_ALREADY_EXISTS);
				return false;
			}
			
			
			msgListener.sendSysMessage(CATEGORY.REGISTRATION, MESSAGE.REQUEST_NEW_PASSWORD);
			
			inputObject = msgListener.connector.readGeneralObject();
			
			if (inputObject instanceof SysMessage) {
				sysMessage = (SysMessage) inputObject;
				if (sysMessage.getMessage() == MESSAGE.SEND_NEW_PASSWORD){
					password = sysMessage.getMessageString();			
				} else {
					msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.MSG_NOT_EXPECTED);
					return false;
				}
			} else {
				msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.WRONG_MSG_TYPE);
				return false;
			}
			
			msgListener.serverObject.userManager.createUser(user, password);
			
			msgListener.sendSysMessage(CATEGORY.REGISTRATION,MESSAGE.REGISTRATION_SUCCESSFUL);
			
			msgListener.serverObject.postInfo(String.format("%s registered at the server", user));
			return true;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return false;
	}

	private boolean userLogin() {
		Object inputObject;
		String user = null, password = null;
		
		msgListener.sendSysMessage(CATEGORY.LOGIN, MESSAGE.REQUEST_USERNAME);
		
		try {
			inputObject = msgListener.connector.readGeneralObject();
		
		
			SysMessage sysMessage;
			if (inputObject instanceof SysMessage) {
				sysMessage = (SysMessage) inputObject;
				if (sysMessage.getMessage() == MESSAGE.SEND_USERNAME){
					user = sysMessage.getMessageString();
				} else {
					msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.MSG_NOT_EXPECTED);
					return false;
				}
			} else {
				msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.WRONG_MSG_TYPE);
				return false;
			}
			
			if (!msgListener.serverObject.userManager.checkUserExists(user)) {
				//TODO check if user is already logged in
				msgListener.sendSysMessage(CATEGORY.LOGIN, MESSAGE.USERNAME_UNKNOWN);
				return false;
			}
			
			
			msgListener.sendSysMessage(CATEGORY.LOGIN, MESSAGE.REQUEST_PASSWORD);
			
			inputObject = msgListener.connector.readGeneralObject();
			
			if (inputObject instanceof SysMessage) {
				sysMessage = (SysMessage) inputObject;
				if (sysMessage.getMessage() == MESSAGE.SEND_PASSWORD){
					password = sysMessage.getMessageString();			
				} else {
					msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.MSG_NOT_EXPECTED);
					return false;
				}
			} else {
				msgListener.sendSysMessage(CATEGORY.ERROR, MESSAGE.WRONG_MSG_TYPE);
				return false;
			}
			
			if (!msgListener.serverObject.userManager.checkUserPassword(user, password)) {
				msgListener.sendSysMessage(CATEGORY.LOGIN, MESSAGE.PASSWORD_WRONG);
				return false;
			} else {
				/*
				int tempsize=msgListener.serverObject.networkListener.messageListener.size()-1;
				
				for(int i=0; i<=tempsize; i++){
					if(msgListener.serverObject.networkListener.messageListener.get(i).username.equals(user)){
						msgListener.sendSysMessage(CATEGORY.LOGIN, MESSAGE.USER_ALREADY_LOGGED_IN);
					return false;}
					
				}*/
				msgListener.sendSysMessage(CATEGORY.LOGIN,MESSAGE.LOGGED_IN);
				
				msgListener.username = user;
				msgListener.loggedIn = true;
				
				SysMessage newUserMessage = new SysMessage(CATEGORY.STATUS, MESSAGE.NEW_USER_INFO, user);
				msgListener.distributeMsg(newUserMessage);
				
				msgListener.serverObject.postInfo(String.format("%s connected to the server", msgListener.username));
				return true;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	

}
